<?php
/*
 *  群发消息调用模块
 *  请在需要调用的模版中使用include标签调用本文件
 *  变量说明：
*/
?>

<!-- massPreviewModal -->
<div id="massPreviewModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="massPreviewModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="massPreviewModalLabel">群发预览</h3>
    </div>
    <div class="modal-body">
        <form id="previewForm" class="form-horizontal">
            <div class="control-group">
                <div class="span3 thumbnail">

                </div>
            </div>
            <div class="control-group">
                <div class="input-append" style="margin-left: 15px">
                    <input class="span2" type="text" id="inputWeixin" name="towxname" placeholder="已关注的微信号">
                    <button id="sendPreview" class="btn btn-primary" type="button">发送预览</button>
                </div>
            </div>
            <input type="hidden" name="media_id" value=""/>
            <input type="hidden" name="type" value=""/>
            <div class="control-group">
                <div class="controls">
                    <div class="alert hidden">

                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
    </div>
</div>
<!-- massMessageModal -->
<div id="massMessageModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="massPreviewModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="massMessageModalLabel">群发消息</h3>
    </div>
    <div class="modal-body">
        <form id="massMessageForm" class="form-horizontal">
            <div class="control-group">
                <div class="span3 thumbnail">

                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <lable>选择标签： </lable>
                    <select name="tag">
                        <option selected value="0">不选择标签</option>
                    </select>
                </div>
                <div class="controls">
                    <button id="massMessage" class="btn btn-primary" type="button">发送</button>
                </div>
            </div>
            <input type="hidden" name="media_id" value=""/>
            <input type="hidden" name="type" value=""/>
            <div class="control-group">
                <div class="controls">
                    <div class="alert hidden">

                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
    </div>
</div>

<script>

    /* 消息预览js */
    var $PreviewForm = $("#previewForm");
    var $PreviewAlert = $PreviewForm.find(".alert");
    //点击预览按钮
    $("a.previewMeterial").click(function(){
        var media_id = $(this).attr("data-id");
        var mptype = $(this).attr('data-type');
        var str = "";
        switch (mptype){
            case 'image':
                str += "<img src='" + $(this).attr('data-path') + "' >";
                break;
            case 'text':
                media_id = $(this).attr('data-text');
                str += "<p>"+media_id+"</p>";
                break;
            default :
                str += "<p>暂时无法展示素材，请发送到手机预览</p>";
                break;
        }
        $PreviewForm.find(".thumbnail").empty().append(str);
        $PreviewForm.find("input[name='media_id']").val(media_id);
        $PreviewForm.find("input[name='type']").val(mptype);
        $PreviewAlert.removeClass('alert-error alert-success').empty().hide();
    });
    //提交预览
    $("#sendPreview").click(function(){
        var towxname = $PreviewForm.find("input[name='towxname']").val();
        if(towxname == ''){
            var msg = "<strong>抱歉!</strong> 请输入要发送的微信帐号.";
            $PreviewAlert.empty().append(msg).show();
        }else{
            $PreviewAlert.removeClass('alert-error').empty().hide();
            var message = $PreviewForm.find("input[name='media_id']").val();
            var mptype = $PreviewForm.find("input[name='type']").val();
            var data = {message:message,towxname:towxname,type:mptype}
            $.post("{:U('Admin/WechatMass/previewMassMessage')}",
                    data,
                    function(re){
                        if(re.status == 0){
                            $PreviewAlert.removeClass("alert-success").addClass('alert-error');
                        }else{
                            $PreviewAlert.removeClass("alert-error").addClass('alert-success');
                        }

                        $PreviewAlert.empty().append(re.info).show();
                    });
        }
    });

    /* 群发js */
    var $MassForm = $("#massMessageForm");
    var $MassAlert = $MassForm.find(".alert");

    //点击群发按钮弹出模态框
    $("a.massMeterial").click(function(){
        var media_id = $(this).attr("data-id");
        var mptype = $(this).attr('data-type');
        var str = "";
        switch (mptype){
            case 'image':
                str += "<img src='" + $(this).attr('data-path') + "' >";
                break;
            case 'text':
                media_id = $(this).attr('data-text');
                str += "<p>"+media_id+"</p>";
                break;
            default :
                str += "<p>暂时无法展示素材，请先发送到手机预览确认后在此处群发</p>";
                break;
        }

        $MassForm.find(".thumbnail").empty().append(str);

        $MassForm.find("input[name='media_id']").val(media_id);

        $MassForm.find("input[name='media_id']").val(media_id);
        $MassForm.find("input[name='type']").val(mptype);
        $MassAlert.removeClass('alert-error alert-success').empty().hide();
    });

    //确定群发
    $("#massMessage").click(function(){
        var tag = $MassForm.find("select[name='tag']").val();
        var message = $MassForm.find("input[name='media_id']").val();
        var mptype = $MassForm.find("input[name='type']").val();
        var data = {tag:tag,message:message,type:mptype}
        $.post("{:U('Admin/WechatMass/sendMassMessage')}",
                data,
                function(re){
                    if(re.status == 0){
                        $MassAlert.removeClass("alert-success").addClass('alert-error');
                    }else{
                        $MassAlert.removeClass("alert-error").addClass('alert-success');
                    }

                    $MassAlert.empty().append(re.info).show();
                });
    });


    var $Tags = {$allTags};
    $($Tags).each(function(index,value){
        var str = "<option value='"+ value.id + "'>" + value.text + "</option>";
        $MassForm.find("select[name='tag']").append(str);
    });

</script>
